{% extends "contest/base.html" %}
{% load full_time %}
{% block contest_page_title%}{% endblock %}
{% block contest_page_body %}
    {% for faq in faqs %}
        <div class="panel panel-info">
            <div class="panel-heading">
                {% if faq.is_notice %}
                    <span class="label label-info">公告</span>
                {% else %}
                    <span class="label label-info">公开问答</span>
                {% endif %}
                &nbsp;&nbsp;
                发起人：{{ faq.author.nickname }} ({{ faq.author.id }}）&nbsp;&nbsp;发起时间：{{ faq.create_time | full_time }}
            </div>
            <div class="panel-body">
                <h4>主题: {{ faq.subject }}</h4>
                {{ faq.content | safe }}
                {% if faq.answer_time != 0 %}
                <hr/>
                <strong>裁判({{ faq.answer_referee.nickname }})回复：</strong><br />
                {{ faq.answer_content }}<br />  <small>于{{ faq.answer_time | full_time }}</small>
                {% endif %}
            </div>
        </div>
    {% endfor %}

    <div id="description_container">{{ contest.description | default:"欢迎参加本次比赛！" | safe }}</div>
    {% if is_referee and user_session.user_id == contest.author.id %}
    <div id="description_hr"><hr /></div>
    <button class="btn btn-primary" id="btn-show-editor">编辑首页内容</button>
    <div id="editor_layout" style="display: none;">
        <form id="contest_setting_form" action="{% url 'contest_mgr_save_contest_setting' contest.id %}?desconly=1" method="post">
            <textarea name="description" id="editor_description">{{ contest.description | default:"" }}</textarea><br />
            <button type="submit" class="btn btn-success">保存</button>
        </form>
    </div>
    {% endif %}
{% endblock %}
{% block contest_page_script %}
    {% if is_referee and user_session.user_id == contest.author.id %}
        <script type="text/javascript" src="/static/library/jstorage.min.js"></script>
        <script type="text/javascript" src="/static/library/sisyphus.min.js"></script>
        <script type="text/javascript" src="/static/ckeditor/ckeditor.js"></script>
        <script type="text/javascript">
        $(function () {
            $("#btn-show-editor").click(function () {
                $(this).hide();
                $("#description_container").hide();
                $("#description_hr").hide();
                $("#editor_layout").show();
                $("#editor_description").val($("#description_container").html());
                CKEDITOR.replace( 'editor_description', { height:'25em' } );
                window.onunload = function () {
                    sis.manuallyReleaseData();
                };

                var sis = $("#contest_setting_form").sisyphus({
                    locationBased: true,
                    timeout: 0,
                    onSave: function () {
                        $(window).bind('beforeunload',function(){
                            return "确定离开此页面吗？未保存的内容将会丢失！";
                        });
                    }
                });
                $("#contest_setting_form").submit(function () {
                for (var ins in CKEDITOR.instances) CKEDITOR.instances[ins].updateElement();
                $.GRestP({
                   responseType: 'json',
                    callback: function (flag, entity) {
                        if(flag){
                            $.AlertBox({
                                body: "比赛首页内容保存成功",
                                color: "success",
                                title: "操作成功",
                                callback: function () {
                                    $.ModalBox({}).hide();
                                    window.location.reload();
                                }
                            }).show();
                        }else{
                            $.AlertBox({body: entity.msg, color: "danger", title: "操作失败"}).show();
                        }
                    }
                }).submit_form(this);
                return false;
            });
            });
        });
        </script>
    {% endif %}
{% endblock %}